import { observable, action } from 'mobx';
import { WeaTableNew, WeaForm } from 'comsMobx';
import * as FrameAPI from '../apis/frame';
import { ComsStore } from './common';
import { message, Modal } from 'antd';
import {
	  WeaTools,
	} from 'ecCom';

const { TableStore } = WeaTableNew;

const confirm = Modal.confirm;

export default class Frame extends ComsStore {
  @observable key = '';
  @observable selectedTreeKeys = [];
  @observable parentid = '';
  @observable showname = '';
  @observable expandedKey = [];
  @observable showSearchAd = false;
  @observable formImport = new WeaForm();// 新建文件夹

  @observable chooseDialogCon = [];// 新建文件夹
  @observable chooseDialogRM = [];// 新建文件夹

  @observable form1 = new WeaForm();// 批量移动
  @observable chooseDialogCon1 = [];// 批量移动
  @observable chooseDialogRM1 = [];// 批量移动

  @observable tempmoveid = '';// 分页组件移动对应id
  @observable tempeditid = '';// 分页组件编辑对应id

  @observable currentid = '';
  @observable reviewid = '';

  @observable dialogtitle = '';// 弹出框的标题

  @observable form2 = new WeaForm();// 上传照片
  @observable chooseDialogCon2 = [];// 上传照片
  @observable chooseDialogRM2 = [];// 上传照片

  @observable chooseDialogRM3 = [];// 相片信息

  @observable form4 = new WeaForm();// 新增评论或者编辑评论
  @observable chooseDialogCon4 = [];// 新增评论或者编辑评论
  @observable chooseDialogRM4 = [];// 新增评论或者编辑评论

  @observable comsWeaTableStore = new TableStore();
  @observable tableStore1 = new TableStore();
  @observable visible = false;
  @observable visible2 = false; // 相片显示
  @observable dialogType = ''; // 根据dialog类型显示对应的元素
  @observable treeData = [];
  @observable tabData = [];
  @observable treeCounts = {};
  @observable quickSearchValue = '';
  @observable albumRate=0;
  @observable operatelevel=0;
  @observable countsType = [];
  @observable loadingTree = false;
  @observable deleteWarm = {};
  @observable albumphotoall = {};
  @observable hasFile = '';

  @action
  setTitle = (title) => {
    this.title = title;
  }
  setKey(key) {
    this.key = key;
    this.parentid = key;
    this.doSearch({ parentid: this.key });
    this.getRightMenu({ parentid: this.key });
    this.form.updateFields({ docsubject: { value: '' } });
    this.quickSearchValue = '';
    this.form.reset();
    FrameAPI.getAlbumSizeInfo({ parentid: this.key }).then((data) => {
		this.albumRate = data.rate;
   	});
   	FrameAPI.getAlbumOperatelevel({ parentid: this.key }).then((data) => {
   		this.operatelevel = data.operatelevel;
   	});
  }

  setSelectedTreeKeys(selectedTreeKeys) {
	  this.selectedTreeKeys = selectedTreeKeys;
  }

  setHasFile(hasFile) {
	  this.hasFile = hasFile;
  }

  doSearch(params = {}) {
	    this.loading = true;
	    FrameAPI.doSearch(params).then((data) => {
	      this.comsWeaTableStore.getDatas(data.sessionkey);
	      this.dataKey = data.sessionkey;
	      this.loading = false;
	      this.doDeleteWarm({});
	    });

	    return this;
	  }

  getConditionInfo(params = {}) {
	    this.conditionLoading = true;
	    FrameAPI.getConditionInfo(params).then((data) => {
	      this.conditionLoading = false;
	      const { isFormInit } = this.form;
	      if (isFormInit) {
	        // 如果初始化过表单，需要重新初始化一份表单，并在同一周期置空数据
	        // 需要暂存数据的可以考虑多份 ComsStore 继承
	        this.form = new WeaForm();
	        this.conditioninfo = [];
	      }
	      this.conditioninfo = data.condition;
	      this.form.initFormFields(data.condition);
	      const keys = Object.keys(params);
	      if (keys.length > 1 && this.form.isFormInit) {
	        const fields = {};
	        keys.forEach((key) => {
	          if (key !== 'viewcondition' && key !== 'groupid') {
	            fields[key] = { value: params[key] };
	          }
	        });
	        this.form.updateFields(fields);
	      }
	    });
	    return this;
	  }

  setParentid = (key) => {
    this.parentid = key;
  }

  setCurrentid = (key) => {
	    this.currentid = key;
  }

  setReviewid = (key) => {
	    this.reviewid = key;
  }

  setShowname = (showname) => {
    this.showname = showname;
  }

  setTempmoveid = (tempmoveid) => {
    this.tempmoveid = tempmoveid;
  }

  setTempeditid = (tempeditid) => {
    this.tempeditid = tempeditid;
  }

  setShowSearchAd = (showSearchAd) => {
    this.showSearchAd = showSearchAd;
  }
  setVisible = (visible) => {
    this.visible = visible;
  }

  setVisible2 = (visible2) => {
	    this.visible2 = visible2;
  }

  setDialogType = (dialogType) => {
    this.dialogType = dialogType;
    if (dialogType === 'upload') {
    	this.getchooseDialogCon2(dialogType);
    }
    if (dialogType === 'onNewFolder' || dialogType === 'onEdit') {
    	this.getchooseDialogCon(dialogType);
    }
    if (dialogType === 'onBatchMove' || dialogType === 'onMove') {
    	this.getchooseDialogCon1(dialogType);
    }
    if (dialogType === 'albumView') {
    	this.getchooseDialogCon3(dialogType);
    }
    if (dialogType === 'addAlbumReview' || dialogType === 'editAlbumReview') {
    	this.getchooseDialogCon4(dialogType);
    }
  }

    saveDialog1(params = {}) {
    	FrameAPI.saveDialog1(params).then((data) => {
    		if (!(this.dialogType === 'onNewFolder' || this.dialogType === 'onEdit')) {
    			this.loading = true;
    			this.key = '';
    			this.getTreeData();
			    this.setTabData([]);
    			this.setExpandedKey([]);
    			this.setShowSearchAd(false);
    			this.setSelectedTreeKeys([]);
    			this.showname = '';
    			this.albumRate = 0;
    		} else {
    			this.doSearch({ parentid: this.parentid });
    		}
    	});
    }

    saveDialog2(params = {}) {
    	FrameAPI.saveDialog1(params).then((data) => {
    		this.tableStore1.getDatas(data.sessionkey1);
    	});
    }

  setTabData = (tabData) => {
    this.tabData = tabData;
  }
  setExpandedKey = (expandedKey) => {
    this.expandedKey = expandedKey;
  }
  setQuickSearchValue = (quickSearchValue) => {
    this.quickSearchValue = quickSearchValue;
  }

  // 新建文件夹或者编辑
  getchooseDialogCon = (params) => {
    FrameAPI.getchooseDialogCon({ dialogType: params, tempeditid: this.tempeditid }).then((data) => {
      this.chooseDialogCon = data.conditioninfo;
      this.formImport.initFormFields(this.chooseDialogCon);
      this.dialogtitle = data.dialogtitle;
      this.chooseDialogRM = data.rightMenu;
    });
  }

  // 批量移动或者单个移动
  getchooseDialogCon1 = (params) => {
    FrameAPI.getchooseDialogCon({ dialogType: params }).then((data) => {
      this.chooseDialogCon1 = data.conditioninfo;
      this.form1.initFormFields(this.chooseDialogCon1);
      this.dialogtitle = data.dialogtitle;
      this.chooseDialogRM1 = data.rightMenu;
    });
  }

  // 上传照片
  getchooseDialogCon2 = (params) => {
    FrameAPI.getchooseDialogCon({ dialogType: params, parentid: this.parentid }).then((data) => {
      this.chooseDialogCon2 = data.conditioninfo;
      this.form2.initFormFields(this.chooseDialogCon2);
      this.dialogtitle = data.dialogtitle;
      this.chooseDialogRM2 = data.rightMenu;
    });
  }

  // 获取相片
  getchooseDialogCon3 = (params) => {
	  FrameAPI.getAlbumPhotoAllById({ photoid: this.currentid }).then((data) => {
		  this.albumphotoall = data;
		  this.chooseDialogRM3 = data.rightMenu;
	      this.tableStore1.getDatas(data.sessionkey1);
	  });
  }


  // 新增评论或者编辑评论
  getchooseDialogCon4 = (params) => {
    FrameAPI.getchooseDialogCon({ dialogType: params, currentid: this.currentid, reviewid: this.reviewid }).then((data) => {
      this.chooseDialogCon4 = data.conditioninfo;
      this.form4.initFormFields(this.chooseDialogCon4);
      this.dialogtitle = data.dialogtitle;
      this.chooseDialogRM4 = data.rightMenu;
    });
  }

  getTreeData = (params) => {
	  this.loadingTree = true;
	  FrameAPI.getTreeData(params).then((data) => {
	      this.treeData = data.treedata;
	      this.treeCounts = data.treecount;
	      this.countsType = data.countcfg;
	      this.doSearch({ parentid: 0 });
	      this.loadingTree = false;
	  });
  }

  // 显示相册还是照片
  albumView = (params) => {
	  FrameAPI.getIsFolder({ currentid: params }).then((data) => {
	      let isFolder = data.isfolder;
	      if (isFolder === '1') {
	    	  this.setParentid(params);
	    	  this.doSearch({ parentid: params });
	      } else {
	    	  this.setCurrentid(params);
	    	  this.setDialogType('albumView');
	    	  this.setVisible2(true);
	      }
	  });
  }

// 删除
  deletePhotoBatch(params = {}) {
	  let parentid1 = this.parentid;
	  this.doDelete(params).then((rdata) => {
		  message.success(rdata.msg);
		  this.getTreeData();
		  this.setTabData([]);
		  this.setExpandedKey([]);
		  this.setShowSearchAd(false);
		  this.setSelectedTreeKeys([]);
		  this.key = '';
		  this.showname = '';
		  this.albumRate = 0;
	  });
  }

  // 删除评论
  deleteAlbumReview(params = {}) {
	  WeaTools.callApi('/api/album/frame/deleteReview', 'GET', params).then((rdata) => {
			message.success(rdata.msg);
			this.tableStore1.getDatas(rdata.sessionkey1);
	  });
  }

  // 删除操作
  doDelete = params => WeaTools.callApi('/api/album/frame/delete', 'GET', params)

  // 获取删除提示信息
  doDeleteWarm(params = {}) {
	  FrameAPI.doDeleteWarm(params).then((data) => {
		  this.deleteWarm = data;
	  });
  }
}

